home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 789 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.3 KB

  1. From: phalpern@truffle.ultranet.com (Pablo Halpern)
  2. Message-ID: <314ec393.5364744@news.ultranet.com>
  3. X-Original-Date: Tue, 19 Mar 1996 15:30:16 GMT
  4. Path: in2.uu.net!bounce-back
  5. Date: 20 Mar 96 03:17:31 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: auto_ptr: no operator bool()?
  9. Organization: UltraNet Communications, Inc.
  10. References: <313ddfd9.16044605@sqarc.sq.com> <4i2jqr$8dl@solutions.solon.com>
  11. X-Newsreader: Forte Agent .99d/16.182
  12. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  13.     iQBFAgUBMU942uEDnX0m9pzZAQFKgQF9Hy2w2YVlB+LLxTxut+06Xnt6oc0s5jVs
  14.     7v9cqrmplF6T4eXeg9V7NyRJnLcW7nv+
  15.     =s7yK
  16.  
  17. James Kanze US/ESC 60/3/141 #40763 <kanze@lts.sel.alcatel.de> wrote:
  18.  
  19. >The real danger is in the following:
  20. >
  21. >    auto_ptr< T >    p1 ;
  22. >    auto_ptr< T >    p2 ;
  23. >
  24. >    if ( p1 == p2 ) ...
  25. >
  26. >You thought you were comparing two pointers, but in fact, you are
  27. >comparing the results of comparing these pointers to null.  The
  28. >expression in the if (illegal with the current definition) will cause
  29. >the conversion operator bool to be called for both pointers.
  30.  
  31. > ...  The `obvious' solution for
  32. >auto_ptr is to add an `isValid' function; in the meantime, just use
  33. >`if ( ptr.get() == NULL )'.  (Another possible alternative is to
  34. >provide a conversion operator to the underlying pointer type, so that
  35. >you can write `if ( ptr == NULL )', just as you would with a normal
  36. >pointer.  I personally don't like the fact that this might result in
  37. >free pointers to the memory without having called a function
  38. >explicitly.) 
  39.  
  40. Why not just have
  41.  
  42. private:
  43.   bool auto_ptr<T>::operator==(const auto_ptr<T>&)
  44.  
  45. public:
  46.   bool auto_ptr<t>::operator==(void *p)
  47.     { return (this->get() == NULL) && (p == NULL); }
  48.  
  49. The latter would return true if and only if both the auto_ptr and the
  50. passed pointer were NULL.
  51. -------------------------------------------------------------
  52. Pablo Halpern                   phalpern@truffle.ultranet.com
  53.  
  54. I am self-employed. Therefore, my opinions *do* represent 
  55. those of my employer.
  56. ---
  57. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  58. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  59. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  60. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  61. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  62.